package com.base.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.user.dao.FriendsDAO;
import com.base.user.entity.Friends;
import com.base.user.service.FriendsService;
import com.mysteel.page.Page;

@Service("friendsService")
public class FriendsServiceImpl implements FriendsService 
{
	@Autowired
	private FriendsDAO friendsDao;

	@Override
	public void addFriends(Friends friends)
	{
		Assert.notNull (friends);
		
		long memberId = friends.getMemberId ();
		long friend_id = friends.getFriend_id ();
		
		Map<String, Object> params = new HashMap<String,Object>(2);
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		Friends f = friendsDao.getByMap (params);
		
		if(ObjectUtils.isNull (f))
		{
			friendsDao.save (friends);
		}
	}

	@Override
	public void deleteFriends(long memberId, long friend_id)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		Friends f = friendsDao.getByMap (params);
		
		if(ObjectUtils.notNull (f))
		{
			friendsDao.delete (f.getId());
		}
	}

	@Override
	public List<Friends> getAllFriendByMemberId(long memberId)
	{
		
		return friendsDao.getAllFriendsByMemberId (memberId);
	}

	@Override
	public boolean isFriend(long memberId, long friend_id)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		Friends f = friendsDao.getByMap (params);
		
		if(ObjectUtils.notNull (f))
		{
			
			return true;
		}
		
		return false;
	}

	@Override
	public Page<Friends> query(Map<String, Object> params, int currentPageNum,int maxPageRowCount)
	{
		Page<Friends> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = friendsDao.count(params);

		if (count > 0)
		{
			page = new Page<Friends>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> FriendsIds = friendsDao.query(params);

			if (ObjectUtils.notEmpty(FriendsIds))
			{
				List<Friends> Friendss = new ArrayList<Friends>(FriendsIds.size());

				for (long FriendsId : FriendsIds)
				{
					Friendss.add(friendsDao.getById (FriendsId));
				}

				page.setDatas(Friendss);
			}
		}

		return page;
	}

	@Override
	public Friends getFriendsByMap(Map<String, Object> params)
	{
		return friendsDao.getByMap (params);
	}
}
